#include "relay.h"

void Electric_GPIO_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(JQC_Clock,ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = JQC_GPIO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	
	GPIO_Init(JQC_PORT,&GPIO_InitStructure);	
}

void Electric_GPIO_Config_2(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(JKC_Clock,ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = JKC_GPIO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	
	GPIO_Init(JKC_PORT,&GPIO_InitStructure);	
}

void Relay_Set_Action(u8 action, u8 value) {
	GPIO_TypeDef *group;
	uint16_t pin;
	switch(action) {
		case 1:
			group = GPIOE;
			pin = GPIO_Pin_0;
			break;
		case 2:
			group = GPIOE;
			pin = GPIO_Pin_1;
			break;
		case 3:
			group = GPIOF;
			pin = GPIO_Pin_1;
			break;
		case 4:
			group = GPIOF;
			pin = GPIO_Pin_2;
			break;
		case 5:
			group = GPIOF;
			pin = GPIO_Pin_3;
			break;
		case 6:
			group = GPIOF;
			pin = GPIO_Pin_4;
			break;
		case 7:
			group = GPIOF;
			pin = GPIO_Pin_5;
			break;
		case 8:
			group = GPIOF;
			pin = GPIO_Pin_6;
			break;
		default:
			return;
	}
	if (value == 0) {
		GPIO_SetBits(group, pin);
		return;
	}
	GPIO_ResetBits(group, pin);
}

void Relay_Init() {
  Electric_GPIO_Config();
  Electric_GPIO_Config_2();
	Relay_Set_Action(1, 0);
	Relay_Set_Action(2, 0);
	Relay_Set_Action(3, 0);
	Relay_Set_Action(4, 0);
	Relay_Set_Action(5, 0);
	Relay_Set_Action(6, 0);
	Relay_Set_Action(7, 0);
	Relay_Set_Action(8, 0);
}
